﻿using UnityEngine;
using System.Collections;

/// <summary>
/// This is the main control script for the player.
/// </summary>
public class ControlBird : MonoBehaviour {

	public InstantiatedSoundScript FlappingBirdSoundObject;
	StopAllPhysics physicsScript;
	GameStarted startedScript;
	bool startedBird = false;

	// Use this for initialization
	void Start () {
		this.physicsScript = (StopAllPhysics)GameObject.Find("Scripts").GetComponent(typeof(StopAllPhysics));
		this.startedScript = (GameStarted)GameObject.Find("Scripts").GetComponent(typeof(GameStarted));
		this.rigidbody2D.isKinematic = true;
	}

	/// <summary>
	/// This part of the script is where the jumping/flapping happens.
	/// </summary>
	void Update () {
		if (!startedBird && this.startedScript.GameHasStarted && !this.physicsScript.GameOver) {
			this.rigidbody2D.isKinematic = false;
			startedBird = true;
		}
		if (JohnnyUIFunctions.IsInTouchState) {
			if (JohnnyUIFunctions.ClickUp) {
				// Do the character jump
				if (!this.physicsScript.GameOver)
				{
					if (!this.startedScript.GameHasStarted) {
						this.startedScript.GameHasStarted = true;
					}
					Vector2 vel = this.rigidbody2D.velocity;
					vel.y = 0f;
					this.rigidbody2D.velocity = vel;
					this.rigidbody2D.AddForce(new Vector2(0f,400f));

					// Do the jump sound
					InstantiatedSoundScript tempSound = (InstantiatedSoundScript)Instantiate(FlappingBirdSoundObject);
					tempSound.StartSound();
				}
			}
		}
	}
}
